package meeting0206;


import node.ListNode;
import node.ListNodeManager;

public class Solution {

    public static void main(String[] args) {
        int[] nums = {1, 2, 2, 1};
        System.out.println(isPalindrome(ListNodeManager.createListNodeByArray(nums)));
    }

    public static boolean isPalindrome(ListNode head) {
        ListNode fast = head;
        ListNode slow = head;
        while (fast != null && fast.next != null) {
            fast = fast.next.next;
            slow = slow.next;
        }

        ListNode tempt = revert(slow);
        while (tempt != null) {
            if (head.val != tempt.val) {
                return false;
            }
            head = head.next;
            tempt = tempt.next;
        }
        return true;
    }

    private static ListNode revert(ListNode root) {
        ListNode pre = null;
        while (root != null) {
            ListNode tempt = root;
            root = tempt.next;

            tempt.next = pre;
            pre = tempt;
        }
        return pre;
    }
}
